package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书v_v");

        int currentSize = bookList.getUsedSize();
        if(currentSize == 0) {
            System.out.println("书架为空，不能删除");
        }

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入书名：");
        String name = scanner.nextLine();

        int index = -1;
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)) {
                index = i;
                break;
            }
        }

        if(index == -1)
        {
            System.out.println("查无此书");
            return;
        }

        for (int i = 0; i < currentSize-1; i++) {
            Book book = bookList.getBook(i+1);
            bookList.setBooks(i,book);
        }

        bookList.setUsedSize(currentSize-1);
    }
}
